/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.test;

import com.alibaba.fastjson.JSON;
import com.test.entry.Order;
import com.test.sink.OrderSink;
import com.test.source.RocketMQConsumer;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.assigners.WindowAssigner;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.Collector;

/**
 * Skeleton for a Flink Streaming Job.
 *
 * <p>For a tutorial how to write a Flink streaming application, check the
 * tutorials and examples on the <a href="http://flink.apache.org/docs/stable/">Flink Website</a>.
 *
 * <p>To package your application into a JAR file for execution, run
 * 'mvn clean package' on the command line.
 *
 * <p>If you change the name of the main class (with the public static void main(String[] args))
 * method, change the respective entry in the POM.xml file (simply search for 'mainClass').
 */
public class RocketStreamingJob {

    public static void main(String[] args) throws Exception {
        // set up the streaming execution environment
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<String> streamSource = env.addSource(new RocketMQConsumer("TopicTest"));
        OrderSink<Order> orderOrderSink = new OrderSink();


        SingleOutputStreamOperator<Order> orderRecords = streamSource
                .filter(s -> StringUtils.isNotEmpty(s))
                .map(s -> JSON.parseObject(s, Order.class)).name("流转商品 pojo");

        orderRecords.addSink(orderOrderSink).name("保存记录");

        orderRecords
                .flatMap(new testToken())
                .keyBy(0)
                .timeWindow(Time.seconds(10L))
                .sum(1)
                .print().name("统计商品");

        //streamSource.flatMap(new testToken()).keyBy(0).sum(1).addSink(orderOrderSink);

        // execute program
        env.execute("统计商品数量");
    }

    public static class testToken implements FlatMapFunction<Order, Tuple2<String, Integer>> {

        @Override
        public void flatMap(Order order, Collector<Tuple2<String, Integer>> collector) throws Exception {
            collector.collect(new Tuple2<>(order.getCode(), 1));
        }
    }
}
